<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

$db = new DB();

$uid = isset($_POST['uid']) ? intval($_POST['uid']) : 0;
$modelid_arr = isset($_POST['modelid']) ? $_POST['modelid'] : 0;
$locked = isset($_POST['locked']) ? 2 : 1;
$created = time();
$updated = $created;

if( $uid < 2 || !is_array($modelid_arr ) ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//检测用户
$user_sql = 'select id from users where uid='.$uid.' and is_del=1 limit 1';
$user_rs = $db->query($user_sql)->getOne();
if( $user_rs['id'] < 2 ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//更新锁定状态
$users_data = array('locked'=>$locked);
$db->table('users')->where('uid='.$uid)->update($users_data)->run();

//删除旧权限记录
$db->table('roles_users')->where('uid='.$uid)->delete()->run();

//插入新权限
foreach( $modelid_arr as $modelid ){
	if( intval($modelid) > 0 ){
		$data=array('uid'=>$uid,
					'modelid'=>intval($modelid),
					'created'=>$created,
					'updated'=>$updated,
					);
		$db->table('roles_users')->insert($data)->run();
	}
}

echo '<script>alert("修改权限成功！");window.location.href="/www/admin/role/user_lists.php";</script>';
exit;